<?php
// $Id: dish.class.php 117 2011-12-28 08:21:23Z tomqin@gmail.com $

defined('IN_ADMIN') or exit('Access Denied!');

class Dish_Controller extends AdminController{
	var $res_id;
	var $storeInfo;

	function init(){
		$this->module = Base::loadModule('admin/dish', true);

		$this->res_id = intval($_REQUEST['res_id']);
		if(empty($this->res_id)){
			$this->renderDialog('参数错误，缺少 res_id', array('返回餐厅列表' => Base::adminUrl('store', 'index')));
		}
		if(!$this->storeInfo = $this->module->getStore($this->res_id)){
			$this->renderDialog('餐厅信息不存在，请返回！', Base::adminUrl('store', 'index'));
		}

		$this->setVar('storeInfo', $this->storeInfo);
		$this->setVar('res_id', $this->res_id);
	}

	function onIndex(){
		$search = array();

		$output = $this->module->listDish($this->res_id, $search);
		list($total, $data, $pagestring) = $output;

		$category = $this->module->listDishCat($this->res_id);

		$this->setVar('category', $category);
		$this->setVar('total', $total);
		$this->setVar('data', $data);
		$this->setVar('pagestring', $pagestring);
		$this->setVar('action', $this->action);
		$this->render('admin/dish_list');
	}

	function onCategory(){
		$category = $this->module->listDishCat($this->res_id);

		$this->setVar('data', $category);
		$this->render('admin/dish_cat');
	}

	function onCreate(){
		$this->setVar('action', $this->action);
		$this->render('admin/dish_edit');
	}

	function onImport(){
		$category = $this->module->listDishCat($this->res_id);

		$this->setVar('category', $category);
		$this->setVar('action', $this->action);
		$this->setVar('res_id', $this->res_id);
		$this->render('admin/dish_import');
	}

	function onModify(){
		$data = $this->module->getDish($this->res_id, $_GET['dish_id']);
		$category = $this->module->listDishCat($this->res_id);

		$this->setVar('data', $data);
		$this->setVar('category', $category);
		$this->setVar('action', $this->action);
		$this->render('admin/dish_edit');
	}

	function onProcess(){
		$operate = array(
			'返回列表页' => Base::adminUrl($this->controller, 'index', array('res_id' => $this->res_id))
		);
		switch(array_get_string($_POST, 'action')){
			case 'create':
				$this->checkData($_POST);
				$dish_id = $this->module->insert($_POST);

				foreach($_POST['price'] as $k => $v) {
					if(!empty($_POST['price_name'][$k]) || !empty($_POST['price'][$k])){
						$price[] = array(
							'dish_id'    => $dish_id,
							'res_id'     => $this->res_id,
							'price_name' => $_POST['price_name'][$k],
							'price'      => $_POST['price'][$k],
						);
					}
				}
				$this->module->changePrice($dish_id, $price);
				$message = '菜品信息添加成功！';
				$operate = array(
					'继续添加信息' => Base::adminUrl($this->controller, 'create', array('res_id' => $this->res_id)),
					'返回列表页'   => Base::adminUrl($this->controller, 'index', array('res_id' => $this->res_id)),
				);
				break;
			case 'modify':
				$this->checkData($_POST);
				$dish_id = array_get_int($_POST, 'dish_id');
				$this->module->editDish($this->res_id, $dish_id, $_POST);
				$message = '菜品信息更新成功！';
				break;
			case 'delete':
				//$this->module->delete($this->res_id);
				$message = '菜品信息删除成功！';
				break;
			case 'import':
				foreach($_POST['name'] as $k => $v) {
					if(!empty($_POST['name'][$k]) || !empty($_POST['price'][$k]) || !empty($_POST['description'][$k])){
						$data[] = array(
							'res_id'     => $this->res_id,
							'dish_cat_id'=> $_POST['cat_id'],
							'name'		 => $_POST['name'][$k],
							'price'      => $_POST['price'][$k],
							'description'=> $_POST['description'][$k],
						);
					}
				}
				$this->module->importDish($this->res_id, $data);
				$message = '菜品信息批量添加成功！';
				$operate = array(
					'继续批量添加' => Base::adminUrl($this->controller, 'import', array('res_id' => $this->res_id)),
					'返回列表页'   => Base::adminUrl($this->controller, 'index', array('res_id' => $this->res_id)),
				);
				break;
		}

		$this->renderDialog($message, $operate);
	}

	function onBatch(){
		$sorts = array();
		if(isset($_POST['select']) && is_array($_POST['select'])){
			foreach($_POST['select'] as $dish_id){
				$sorts[$dish_id] = intval($_POST['listorder'][$dish_id]);
				$dishcats[$dish_id] = array(
					'catname' => $_POST['dish_cat_name'][$dish_id],
					'ordnum'  => $sorts[$dish_id],
				);
			}
		}

		$action  = 'index';
		switch(array_get_string($_POST, 'action')){
			case 'sort':
				$this->module->sortArray($sorts);
				$message = '菜品信息批量排序设置成功！';
				break;
			case 'enable':
				$this->module->statusArray(array_keys($sorts), 1);
				$message = '菜品信息批量审核操作成功！';
				break;
			case 'disable':
				$this->module->statusArray(array_keys($sorts), 0);
				$message = '菜品信息取消审核操作成功！';
				break;
			case 'delete':
				$this->module->deleteArray(array_keys($sorts));
				$message = '菜品信息删除成功！';
				break;
			case 'change':
				$this->module->changeArray(array_keys($sorts), array_get_int($_POST, 'change_dish_cat'));
				$message = '菜品分类变更操作成功！';
				break;
			case 'addcat':
				$this->module->addDishCat($this->res_id, array_get_string($_POST, 'new_dish_cat'));
				$message = '菜品分类添加成功！';
				$action  = 'category';
				break;
			case 'editcat':
				$this->module->editDishCatArray($this->res_id, $dishcats);
				$message = '菜品分类更新成功！';
				$action  = 'category';
				break;
			case 'delcat':
				$this->module->delDishCatArray($this->res_id, $sorts);
				$message = '菜品分类删除成功！';
				$action  = 'category';
				break;
		}

		$this->renderDialog($message, array('返回列表页' => Base::AdminUrl($this->controller, $action, array('res_id' => $this->res_id))));
	}

	private function checkData(){

	}

	function __destruct(){
		//print_r($this->db);
		//print_r($_POST);
	}
}
?>